﻿package com.unifiedfield{
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	public class SonyBG {
		var allcolors
		var colors1;
		var alphas1;
		var ratios1;
		var colors2;
		var alphas2;
		var ratios2;
		var gradient_mc;
		var myGrad:GradientTween;
		var gradWidth;
		var gradHeight;
		var realgradHeight;
		var gradModeRotation;
		var i;
		var up;
		var angle;
		var at;
		public function SonyBG():void {
			gradient_mc=new MovieClip();
			at=0;
			i=0;
			up=true;
		}
		public function changeColorTo(c){
			allcolors=c;
			myGrad = new GradientTween(colors1, alphas1, ratios1, colors2, alphas2, ratios2, gradModeRotation, gradWidth, gradHeight, gradient_mc, false);
		}
		public function createGradientAnim(colors,ratios:Array,Wid:uint,Hei:uint,Alph:uint,ang:uint,speed:uint,container:MovieClip):void {
			allcolors=colors;
			angle=ang;
			gradient_mc.alpha = Alph;
			nextColor(1);
			nextColor(2);
			alphas2 = alphas1 = [100, 100, 100];
			ratios2 = ratios1 = ratios;
			gradModeRotation = "linear";
			gradWidth = Wid;
			realgradHeight = Hei;
			gradHeight = speed;
			myGrad = new GradientTween(colors1, alphas1, ratios1, colors2, alphas2, ratios2, gradModeRotation, gradWidth, gradHeight, gradient_mc, false);
			gradient_mc.x = 0;
			gradient_mc.y = 0;
			gradient_mc.addEventListener(Event.ENTER_FRAME, animate);
			container.addChildAt(gradient_mc,0);
		}
		private function nextColor(num:uint) {
			if (at>=allcolors.length) {
				at=0;
			}
			var one=at;
			var two=(at+1)%allcolors.length;
			var three=(at+2)%allcolors.length;
			//trace(one,two,three);
			if(one>=allcolors.length){
				one-=allcolors.length;
			}
			if(two>=allcolors.length){
				two-=allcolors.length;
			}
			if(three>=allcolors.length){
				three-=allcolors.length;
			}
			if (num==1) {
				colors1=[allcolors[one],allcolors[two],allcolors[three]];
			} else {
				colors2=[allcolors[one],allcolors[two],allcolors[three]];
			}
			at++;
		}
		private function animate(ev:Event):void {
			var matr:Matrix = new Matrix();
			var mg;
			// draw gradient fill
			mg = myGrad.multiGradient[i];
			//if (i%3==0) {
				angle+=0.1;
				matr.identity();
				matr.createGradientBox(1920,1200,angle*2*Math.PI/360,0,0);
				gradient_mc.graphics.clear();
				gradient_mc.graphics.beginGradientFill(mg.gradModeRotation,mg.colors,mg.alphas,mg.rotation,matr);
				gradient_mc.graphics.drawRect(0,0,gradWidth,realgradHeight);
			//}
			// increment counter
			if (up) {
				i++;
			} else {
				i--;
			}
			if (i == gradHeight-1) {
				up = !up;
				nextColor(1);
				myGrad = new GradientTween(colors1, alphas1, ratios1, colors2, alphas2, ratios2, gradModeRotation, gradWidth, gradHeight, gradient_mc, false);
			} else if (i == 0) {
				up = !up;
				nextColor(2);
				myGrad = new GradientTween(colors1, alphas1, ratios1, colors2, alphas2, ratios2, gradModeRotation, gradWidth, gradHeight, gradient_mc, false);
			}
		}
	}
}
import flash.display.MovieClip;
class GradientTween extends MovieClip {

	public var Width:Number;
	public var Height:Number;
	public var multiGradient:Array;

	private var r:Array;
	private var r1c:Array;
	private var c1c:Array;
	private var a1c:Array;
	private var r2c:Array;
	private var c2c:Array;
	private var a2c:Array;
	private var rLen:Number;

	private var draw:Boolean;

	private var rT:Array;

	function GradientTween(
	   colors1:Array,
	   alphas1:Array,
	   ratios1:Array,   
	   colors2:Array,
	   alphas2:Array,
	   ratios2:Array,   
	   gradModeRotation:String,
	   Width:Number,
	   Height:Number,
	   canvas:MovieClip,
	   draw:Boolean) {

		var aC,alphas,c,cC,colors,endC,eI,eV,hex,i,j,k,len,rC,rotation,sI,searchV,startC,sV,t,angle;

		if (draw != false) {
			draw = true;
		}
		this.Width = Width;
		this.Height = Height;

		r = ratios1.concat(ratios2);
		r.sort(16);

		multiGradient = new Array();

		for (i=1, len=r.length; i<len; i++) {
			if (r[i] == r[i-1]) {
				r.splice(i,1);
				len--;
				i--;
			}
		}
		rLen = r.length;

		angle=0;
		r1c = ratios1.slice(0);
		c1c = colors1.slice(0);
		a1c = alphas1.slice(0);

		r2c = ratios2.slice(0);
		c2c = colors2.slice(0);
		a2c = alphas2.slice(0);

		for (i=0, len=c1c.length; i<len; i++) {
			hex = c1c[i];
			c1c[i] = {r:(hex & 0xFF0000) >> 16, g:(hex & 0x00FF00) >> 8, b:hex & 0x0000FF};
		}
		for (i=0, len=c2c.length; i<len; i++) {
			hex = c2c[i];
			c2c[i] = {r:(hex & 0xFF0000) >> 16, g:(hex & 0x00FF00) >> 8, b:hex & 0x0000FF};
		}
		for (k=0; k<2; k++) {
			aC = k ? a1c : a2c;
			rC = k ? r1c : r2c;
			cC = k ? c1c : c2c;

			// Find the new ratios and push in a flag for the new colors
			for (i=0; i<rLen; i++) {
				if (r[i] != rC[0]) {
					cC.splice(i,0,'new');
					aC.splice(i,0,'new');
				}
				if (rC[0] <= r[i]) {
					rC.splice(0,1);
				}
			}
			for (i=0; i<rLen; i++) {
				if (cC[i] == 'new') {
					// find the neighbours which already have a color value
					sI = i-1;
					for (j=i+1; j<rLen; j++) {
						if (cC[j] != 'new') {
							eI = j;
							break;
						}
					}
					// find t of the new ratio, relative to the neighbouring 'old' ratios
					sV = r[sI];
					searchV = r[i];
					eV = r[eI];
					t = (searchV-sV)/(eV-sV);

					// linear
					startC = cC[sI];
					endC   = cC[eI];
					cC[i] ={r: (1-t)*startC.r + t*endC.r, g: (1-t)*startC.g + t*endC.g, b: (1-t)*startC.b + t*endC.b};
					aC[i] = (1-t)*aC[sI] + t*aC[eI];
				}
			}
		}
		colors = new Array();
		alphas = new Array();

		// draw for each pixel of the height 1 gradient
		for (i=0; i<Height; i++) {
			// linear
			t = i/Height;
			for (j=0; j<rLen; j++) {
				startC = c1c[j];
				endC   = c2c[j];
				colors[j] = ((1-t)*startC.r + t*endC.r << 16) + ((1-t)*startC.g + t*endC.g << 8) + (1-t)*startC.b + t*endC.b;
				alphas[j] = (1-t)*a1c[j] + t*a2c[j];
				//rotation = (1-t)*rotation1 + t*rotation2;
			}
			// these sub-clips are really necessary only if one of the alphas is reduced
			/*var c=new MovieClip();
			c.name = "c"+i;
			canvas.addChild(c);*/
			multiGradient[i] = {};
			multiGradient[i].gradModeRotation = gradModeRotation;
			multiGradient[i].colors = colors.slice(0);
			multiGradient[i].alphas = alphas.slice(0);
			multiGradient[i].rotation = r;
		}
	}
}